﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using XinYuan.Office.Models;
using XinYuanOfficeWinForm.Admin.Code;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System.Collections;

namespace XinYuanOfficeWinForm.Admin.Permission
{
    public partial class FrmRights : Form
    {
        OperatorServiceReference.OperatorServiceClient operatorClient = new OperatorServiceReference.OperatorServiceClient();
        RoleServiceReference.RoleServiceClient roleClient = new RoleServiceReference.RoleServiceClient();
        public FrmRights()
        {
            InitializeComponent();
            dgvOperatorList.AutoGenerateColumns = false;
        }

        private void FrmRights_Load(object sender, EventArgs e)
        {
            Form f = null;
            if (TraverseForm("FrmMain", ref f))
            {
                TraverseMenu(f);
            }
            BindOperator();
        }


        #region Method
        bool TraverseForm(string str, ref Form f)
        {
            Assembly a = Assembly.LoadFile(Application.ExecutablePath);
            Type[] types = a.GetTypes();
            foreach (Type t in types)
            {
                Console.WriteLine(t.Name);
                if (t.BaseType != null && t.BaseType.Name == "Form")
                {
                    f = (Form)Activator.CreateInstance(t, true);
                    if (f != null)
                    {
                        if (f.Name == str)
                        {
                            return true;
                        }
                    }
                }
            }
            return false;
        }
        void TraverseMenu(Control control)
        {
            Admin.Code.FormMenuList.AllMenu.Clear();
            foreach (Control c in control.Controls)
            {
                if (c is MenuStrip)
                {
                    MenuStrip menu = c as MenuStrip;
                    //遍历一级菜单
                    for (int i = 0; i < menu.Items.Count; i++)
                    {
                        ToolStripMenuItem m = menu.Items[i] as ToolStripMenuItem;
                        Admin.Code.FormMenu formMenu = new Code.FormMenu();
                        formMenu.MenuID = m.Name;
                        formMenu.EnableState =m.Enabled ;
                        formMenu.MenuParentID = "";
                        formMenu.MenuName = m.Text;
                        Admin.Code.FormMenuList.AddMenu(formMenu);
                        tvwRightView.Nodes.Add(menu.Items[i].Name, menu.Items[i].Text);
                        //遍历二级菜单
                        for (int j = 0; j < m.DropDownItems.Count; j++)
                        {
                            Admin.Code.FormMenu subMenu = new Code.FormMenu();
                            if (m.DropDownItems[j].GetType().Name == "ToolStripSeparator")
                            {
                                tvwRightView.Nodes[i].Nodes.Add(m.DropDownItems[j].Name, "-------------");
                                tvwRightView.Nodes[i].Nodes[m.DropDownItems[j].Name].ForeColor = Color.Red;
                                tvwRightView.Nodes[i].Nodes[m.DropDownItems[j].Name].ToolTipText = "分割线";
                                subMenu.MenuName = "-------------";
                            }
                            else
                            {
                                tvwRightView.Nodes[i].Nodes.Add(m.DropDownItems[j].Name, m.DropDownItems[j].Text);
                                tvwRightView.Nodes[i].Nodes[m.DropDownItems[j].Name].ToolTipText = m.DropDownItems[j].Text;
                                subMenu.MenuName = m.DropDownItems[j].Text;
                            }
                            subMenu.MenuParentID = formMenu.MenuID;
                            subMenu.MenuID = m.DropDownItems[j].Name;
                            subMenu.EnableState = m.DropDownItems[j].Enabled;
                            Admin.Code.FormMenuList.AddMenu(subMenu);
                            Console.WriteLine(m.DropDownItems[j].Name+""+m.DropDownItems[j].Enabled);
                        }

                    }
                }
            }
            tvwRightView.ExpandAll();
        }
        void BindOperator()
        {
            List<Operator> listOperator = operatorClient.GetOperators();
            dgvOperatorList.DataSource = listOperator;
        }
        #endregion

       

        private void dgvOperatorList_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            for (int i = 0; i < dgvOperatorList.Rows.Count; i++)
            {
                dgvOperatorList.Rows[i].HeaderCell.Value = (i + 1) + "";
            }
        }

        private void tsbtnAddOperator_Click(object sender, EventArgs e)
        {
            FrmEditOperator frmEditOperator = new FrmEditOperator();
            frmEditOperator.ShowDialog();
        }

        private void tsbtnRefreshOperator_Click(object sender, EventArgs e)
        {
            BindOperator();
        }

        private void tsbClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void tsbtnDeleteOperator_Click(object sender, EventArgs e)
        {
            if (dgvOperatorList.CurrentRow == null) return;
            if (DialogResult.OK == MessageBox.Show("确认要删除操作员" + dgvOperatorList.CurrentRow.Cells["OperatorName"].Value + "吗？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk))
                try
                {
                    int id = Convert.ToInt32(dgvOperatorList.CurrentRow.Cells["OperatorId"].Value);
                    if (operatorClient.Delete(id) == 1)
                    {
                        MessageBox.Show("删除成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
                catch
                {
                    throw new Exception("删除出现异常");
                }
        }

        private void tsbtnRoleManager_Click(object sender, EventArgs e)
        {
            FrmRightsGroup frmRightsGroup = new FrmRightsGroup();
            frmRightsGroup.MdiParent = this.ParentForm;
            frmRightsGroup.Show();
        }

        private void dgvOperatorList_Click(object sender, EventArgs e)
        {
            if (dgvOperatorList.CurrentRow!=null)
            {
                int id = Convert.ToInt32(dgvOperatorList.CurrentRow.Cells["OperatorId"].Value);
                string func = roleClient.GetFunctionByOperatorId(id);
                CheckFunction(func);
            }
        }
        private void CheckFunction(string function)
        {
            for (int i = 0; i < tvwRightView.Nodes.Count; i++)
            {
                tvwRightView.Nodes[i].Checked = false;
                if (function.Contains(tvwRightView.Nodes[i].Name))
                {
                    tvwRightView.Nodes[i].Checked = true;
                }
                for (int j = 0; j < tvwRightView.Nodes[i].Nodes.Count; j++)
                {
                    tvwRightView.Nodes[i].Nodes[j].Checked = false;
                    if (function.Contains(tvwRightView.Nodes[i].Nodes[j].Name))
                    {
                        tvwRightView.Nodes[i].Nodes[j].Checked = true;
                    }
                }
            }
        }
       
    }
}
